package com.example.softwareengg;




import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.StringTokenizer;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;


public class ScreenUsage extends HeaderFooter implements OnClickListener{
	String ipadd;
	String serverIP = MyOverridenApp.getserverIP();
String ipdefault = "127.0.0.1";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewGroup vg = (ViewGroup) findViewById(R.id.lldata);
        ViewGroup.inflate(ScreenUsage.this, R.layout.screenusage, vg);
        TextView userName1 = (TextView)findViewById(R.id.textUsername);
        	
      		
      	
      
		//Intent i1 = getIntent();
		String shruti2 = "Welcome " +MyOverridenApp.getUser();
		TextView UsageHead = (TextView) findViewById(R.id.usagehead);
		//Button browse = (Button)findViewById(R.id.setip);
		SpannableString content1 = new SpannableString(" Data Usage ");
		SpannableString content2 = new SpannableString(shruti2);
		content1.setSpan(new UnderlineSpan(), 0, content1.length(), 0);
content2.setSpan(new UnderlineSpan(), 0, content2.length(), 0);
		userName1.setText(content2);
		UsageHead.setText(content1);
		String serverReply = null;
		TextView uploaded = (TextView)findViewById(R.id.usagevalueuploaded);
		TextView downloaded = (TextView)findViewById(R.id.usagevaluedownloaded);
		int flag = 0;
		flag = 1;//remove this
		try{
		Socket client = MyOverridenApp.getClient();
		DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
		//Toast.makeText(getApplicationContext(),username+password1+"asgjhg", Toast.LENGTH_LONG).show();
		outToServer.writeBytes("usage\n");
		
		
		BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
		
		serverReply = inFromServer.readLine();}catch(Exception e){
			e.printStackTrace();
		}					
		//progressDialog.dismiss();
		
	if(!serverReply.equals(""))
		flag = 1;
	
	if(flag == 1)
	{
		StringTokenizer str = new StringTokenizer(serverReply," ");
		String down = str.nextToken();
		String up = str.nextToken();
		//up = down = "100 kb";
		uploaded.setText(uploaded.getText().toString()+up+"kb.");
		downloaded.setText(downloaded.getText().toString()+down+"kb.");
	}
		
	Button button_logout = (Button) findViewById(R.id.Button_logout);
	 Button home = (Button)findViewById(R.id.home);
	
	button_logout.setOnClickListener(this);
	home.setOnClickListener(this);
	}
	
	 public void onClick(View v) {
	        switch (v.getId()) {
	        
	        case R.id.home:
	        {
				   Intent intent = new Intent(ScreenUsage.this,ScreenB.class);
				    startActivity(intent);
				    break;

	        }
	        
	      	      	      	      case R.id.Button_logout:
	  	        {
	  	        	try{
	  	        	Socket client = MyOverridenApp.getClient();
	  	        	DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
	  				
	  				outToServer.writeBytes("signout\n");
	  				client.close();
	  				}
	  	        	catch(Exception e){
	  	        		e.printStackTrace();
	  	        	}
	  				   Intent intent = new Intent(ScreenUsage.this,ScreenA.class);
	  				    startActivity(intent);
	  				    break;

	  	        }
	  	        default:
	  	        	break;
	  	        }
	
}
	 @Override
	 public void onBackPressed() {
	 }
	 }